package com.mhz.cae.hud;

import java.util.List;

import org.anddev.andengine.engine.camera.hud.HUD;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.util.modifier.ease.EaseStrongOut;

import com.mhz.cae.CausesAndEffectsActivity;
import com.mhz.cae.objects.ItemType;

public class Inventory {

	private final static int POS_Y = 70;
	private final static int WIDTH = 150;
	private final static EaseStrongOut EASE_STRONG_OUT = EaseStrongOut.getInstance();

	private boolean displayed = false;

	private final Shape container;

	private List<ItemType> items;

	public static Inventory createInventory(final HUD hud) {
		return new Inventory(hud);
	}

	private Inventory(final HUD hud) {
		container = new Rectangle(CausesAndEffectsActivity.CAMERA_WIDTH, POS_Y, WIDTH, CausesAndEffectsActivity.CAMERA_HEIGHT - POS_Y);
		hud.attachChild(container);
	}

	public void setItems(List<ItemType> items) {
		this.items = items;
	}

	public void openClose() {
		if (displayed) {
			hide();
		} else {
			show();
		}
	}

	public void show() {
		if (!displayed) {
			container.clearEntityModifiers();
			container.registerEntityModifier(new MoveModifier(2, container.getX(), CausesAndEffectsActivity.CAMERA_WIDTH - WIDTH, container.getY(), POS_Y,
					EASE_STRONG_OUT));
			displayed = true;
		}
	}

	public void hide() {
		if (displayed) {
			container.clearEntityModifiers();
			container.registerEntityModifier(new MoveModifier(2, container.getX(), CausesAndEffectsActivity.CAMERA_WIDTH, container.getY(), POS_Y,
					EASE_STRONG_OUT));
			displayed = false;
		}
	}

	// TODO initialiser les fonts autrement
	public void initItems(Font font, HUD pHud) {
		final float BOX_SPACING = 5;
		float boxY = 5;
		ItemBox box = null;
		for (ItemType item : items) {
			box = new ItemBox(item.getName(), font, boxY);
			box.setNumberItem(item.getCurrentNumber());
			box.setUserData(item);
			pHud.registerTouchArea(box);
			container.attachChild(box);
			boxY += box.getHeight() + BOX_SPACING;
		}
	}
}
